/*
 * ArraysNeighbouringNumbers.java   1.0 Nov 4, 2013
 *
 * Find two neighbouring numbers in an array with the smallest distance to each
 * other.
 * The index of the first number should be outputted to the screen.
 * For example, in the sequence 4 8 6 1 2 9 4 the minimum distance is 1 (between
 * 1 and 2).
 * The function should return the index 3 (of number 1).
 *
 * If ecxists more than one decision should be outputted only a first one.
 */


public class ArraysNeighbouringNumbers {
    public static void main(String[] args) {
        int[] numberSequence
            = Commons.getIntArrayWithPrompt(Messages.ENTER_INT_ARRAY);

        if (numberSequence.length < 2) {
            Commons
                .terminateProgramWithMessage(Messages.YOU_MUST_INPUT_TWO_ELEMENTS);
        }

        System.out.println(
                Messages.FIRST_NUMBER_INDEX
                    + findSmallestDistanceIndex(numberSequence));
    }

    public static int findSmallestDistanceIndex(int[] sequence) {
        int distance = 0;
        int smallestDistance = -1;
        int smallestDistanceIndex = 0;

        for (int i = 1; i < sequence.length; i++) {
            distance = Math.abs(sequence[i] - sequence[i-1]);
            if ((smallestDistance < 0) || (distance < smallestDistance)) {
                smallestDistance = distance;
                smallestDistanceIndex = i - 1;
            }
        }
        return smallestDistanceIndex;
    }
}
